package dao;

import dao.imp.IContractDao;
import entity.Contract;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.MysqlConnUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: personnel_management_system
 * @description: contract relation
 * @author: Mr.Sun
 * @create: 2020-05-25 14:41
 **/

public class ContractDao  implements IContractDao {
    JdbcTemplate template = new JdbcTemplate(MysqlConnUtil.getDataSource());

    public ContractDao() throws Exception {
    }

    @Override
    public boolean insert(Contract contract) {

        boolean res = false;
        String sql = "INSERT INTO contract(type,date,duration,emp_id,work_time) VALUES(?,?,?,?,?)";

        int count = template.update(sql,contract.getType(),contract.getDate(),contract.getDuration(),contract.getEmp_id(),contract.getWork_time());
        res = count == 1;
        return res;
    }

    @Override
    public boolean update(Contract contract) {
        boolean res = false;
        String sql = "UPDATE contract SET type = ?,date =?,duration = ?,emp_id = ? ,work_time = ? WHERE c_id = ?";

        int count = template.update(sql,contract.getType(),contract.getDate(),contract.getDuration(),contract.getEmp_id(),contract.getWork_time(),contract.getC_id());
        res = count == 1;
        return res;
    }

    @Override
    public boolean delete(Contract contract) {
        boolean res =false;
        String sql = "DELETE FROM contract WHERE c_id = ?";
        int count = template.update(sql,contract.getC_id());
        return count == 1;
    }


    @Override
    public List<Contract> selectAll() {
        List<Contract> contracts = new ArrayList<>();
        String sql = "select * from contract";
        contracts = template.query(sql,new BeanPropertyRowMapper<Contract>(Contract.class));
        return contracts;
    }

    @Override
    public List<Contract> select(Contract contract) {

        return null;
    }
}
